package org.x.common.web.utils;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.x.api.system.dto.LoginLogDTO;
import org.x.api.system.feign.RemoteLogService;
import org.x.common.base.result.ResultCode;

import java.time.LocalDateTime;

/**
 * 发送远程日志
 */
@Slf4j
public class RemoteLogKit {

    public static void sendLoginLog(RemoteLogService remoteLogService,String username, ResultCode resultCode) {
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
            LoginLogDTO logDTO = new LoginLogDTO();
            logDTO.setUserId(username);
            // 使用Hutool获取客户端IP（支持代理场景）
            logDTO.setIp(HttpKit.getClientIP(request));
            logDTO.setStatus((byte) (resultCode.getCode() == 200 ? 1 : 0));
            logDTO.setCreateTime(LocalDateTime.now());
            logDTO.setRemark(resultCode.getMsg());
            remoteLogService.recordLoginLog(logDTO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

    }

    public static void sendOperateLog(RemoteLogService remoteLogService){

    }

    public static void sendSystemLog(RemoteLogService remoteLogService){

    }


}
